package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessRecordDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.Item;
import com.jyu.apts.entity.ItemBatchRecord;
import com.jyu.apts.entity.ItemProcess;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.QRCode;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.ItemBatchRecordStatusEnum;
import com.jyu.apts.entityEnum.QRCodeStatusEnum;
import com.jyu.apts.entityEnum.QRCodeTypeEnum;
import com.jyu.apts.service.ItemBatchRecordService;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.ItemProcessService;
import com.jyu.apts.service.ItemProductionLineService;
import com.jyu.apts.service.ItemService;
import com.jyu.apts.service.QRCodeService;
import com.jyu.apts.service.UserService;
import com.jyu.apts.utils.DateUtil;

@RestController
public class OrganizationItemProcessRecordController {

	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	@Autowired
	ItemProcessService itemProcessService;
	@Autowired
	ItemBatchRecordService itemBatchRecordService;
	@Autowired
	ItemService itemService;
	@Autowired
	ItemProductionLineService itemProductionLineService;
	@Autowired
	UserService userService;
	@Autowired
	QRCodeService qrCodeService;
	
	@RequiresPermissions("organizationItemProcessRecord_html")
	@RequestMapping("/organizationItemProcessRecord_html")
	public ModelAndView organizationItemProcessRecord_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationItemProcessRecord.html");
		return mv;
	}
	
	@RequiresPermissions("organizationItemProcessRecord_html")
	@RequestMapping("/addOrganizationItemProcessRecord")
	public ResultDto addOrganizationItemProcessRecord(long operatorId,long itemProcessId,long itemBatchRecordId,String normalQRCode) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		User operator = userService.getOne(operatorId);
		if(operator == null) {
			return new ResultDto("操作员不存在",null,2);
		}
		if(operator.getOrganization().getId()!=organization.getId()) {
			return new ResultDto("所填写操作员非本机构人员",null,2);
		}
		ItemProcess itemProcess = itemProcessService.getOne(itemProcessId);
		if(itemProcess == null) {
			return new ResultDto("流程不存在",null,2);
		}
		ItemProductionLine itemProcess_itemProductionLine = itemProcess.getItemProductionLine();
		
		if(itemProcess_itemProductionLine.getItem().getOrganization().getId()!=organization.getId()) {
			return new ResultDto("所填写操作流程非本机构流程",null,2);
		}
		ItemBatchRecord itemBatchRecord = itemBatchRecordService.getOne(itemBatchRecordId);
		if(itemBatchRecord == null) {
			return new ResultDto("商品批次不存在",null,2);
		}
		ItemProductionLine itemBatchRecord_itemProductionLine = itemBatchRecord.getItemProductionLine();
		if(itemBatchRecord_itemProductionLine.getId()!=itemProcess_itemProductionLine.getId()) {
			return new ResultDto("流程与批次并非同一生产线",null,2);
		}
		ItemProcessRecord itemProcessRecord = new ItemProcessRecord();
		if(normalQRCode!=null && normalQRCode.trim().length()!=0) {
			QRCode qrCode = qrCodeService.findByCode(normalQRCode);
			if(qrCode == null) {
				return new ResultDto("二维码不存在",null,2);
			}
			if(qrCode.getStatus()!=QRCodeStatusEnum.UNUSED.getType()) {
				return new ResultDto("二维码已经被使用",null,2);
			}
			if(qrCode.getType()!=QRCodeTypeEnum.NORMAL.getType()) {
				return new ResultDto("二维码类型错误,并非普通二维码",null,2);
			}
			if(qrCode.getAuthor().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("非本机构人员创建二维码",null,2);
			}
			qrCode.setStatus(QRCodeStatusEnum.USED.getType());
			qrCodeService.save(qrCode);
			itemProcessRecord.setNormalQRCode(qrCode);
		}
		itemProcessRecord.setItemBatchRecord(itemBatchRecord);
		itemProcessRecord.setItemProcess(itemProcess);
		itemProcessRecord.setOperatorId(operator.getId());
		itemProcessRecord.setOperatorNickname(operator.getNickname());
		itemProcessRecord.setCreatetime(DateUtil.getYYYY_MM_DD_HH_MM_SS_Format().format(new Date()));
		itemProcessRecordService.save(itemProcessRecord);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProcessRecord_html")
	@RequestMapping("/upOrganizationItemProcessRecord")
	public ResultDto upOrganizationItemProcessRecord(long id,long operatorId,long itemProcessId,long itemBatchRecordId,String normalQRCode) {
		ItemProcessRecord organizationItemProcessRecord = itemProcessRecordService.getOne(id);
		if(organizationItemProcessRecord == null) {
			return new ResultDto("流程记录不存在!",null,2);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		User operator = userService.getOne(operatorId);
		if(operator == null) {
			return new ResultDto("操作员不存在",null,2);
		}
		if(operator.getOrganization().getId()!=organization.getId()) {
			return new ResultDto("所填写操作员非本机构人员",null,2);
		}
		ItemProcess itemProcess = itemProcessService.getOne(itemProcessId);
		if(itemProcess == null) {
			return new ResultDto("流程不存在",null,2);
		}
		ItemProductionLine itemProcess_itemProductionLine = itemProcess.getItemProductionLine();
		
		if(itemProcess_itemProductionLine.getItem().getOrganization().getId()!=organization.getId()) {
			return new ResultDto("所填写操作流程非本机构流程",null,2);
		}
		ItemBatchRecord itemBatchRecord = itemBatchRecordService.getOne(itemBatchRecordId);
		if(itemBatchRecord == null) {
			return new ResultDto("商品批次不存在",null,2);
		}
		ItemProductionLine itemBatchRecord_itemProductionLine = itemBatchRecord.getItemProductionLine();
		if(itemBatchRecord_itemProductionLine.getId()!=itemProcess_itemProductionLine.getId()) {
			return new ResultDto("流程与批次并非同一生产线",null,2);
		}
		if(normalQRCode!=null && normalQRCode.trim().length()!=0) {
			QRCode db_qrCode = organizationItemProcessRecord.getNormalQRCode();
			if(db_qrCode==null) {
				QRCode qrCode = qrCodeService.findByCode(normalQRCode);
				if(qrCode == null) {
					return new ResultDto("二维码不存在",null,2);
				}
				if(qrCode.getStatus()!=QRCodeStatusEnum.UNUSED.getType()) {
					return new ResultDto("二维码已经被使用",null,2);
				}
				if(qrCode.getType()!=QRCodeTypeEnum.NORMAL.getType()) {
					return new ResultDto("二维码类型错误,并非普通二维码",null,2);
				}
				if(qrCode.getAuthor().getOrganization().getId()!=organization.getId()) {
					return new ResultDto("非本机构人员创建二维码",null,2);
				}
				qrCode.setStatus(QRCodeStatusEnum.USED.getType());
				qrCodeService.save(qrCode);
				organizationItemProcessRecord.setNormalQRCode(qrCode);
			}
			else if(!db_qrCode.getCode().equals(normalQRCode)) {
				QRCode qrCode = qrCodeService.findByCode(normalQRCode);
				if(qrCode == null) {
					return new ResultDto("二维码不存在",null,2);
				}
				if(qrCode.getStatus()!=QRCodeStatusEnum.UNUSED.getType()) {
					return new ResultDto("二维码已经被使用",null,2);
				}
				if(qrCode.getType()!=QRCodeTypeEnum.NORMAL.getType()) {
					return new ResultDto("二维码类型错误,并非普通二维码",null,2);
				}
				db_qrCode.setStatus(QRCodeStatusEnum.UNUSED.getType());
				if(qrCode.getAuthor().getOrganization().getId()!=organization.getId()) {
					return new ResultDto("非本机构人员创建二维码",null,2);
				}
				qrCode.setStatus(QRCodeStatusEnum.USED.getType());
				List<QRCode> upQrCodes = new ArrayList<QRCode>();
				upQrCodes.add(qrCode);
				upQrCodes.add(db_qrCode);
				qrCodeService.save(upQrCodes);
				organizationItemProcessRecord.setNormalQRCode(qrCode);
			}
		}
		organizationItemProcessRecord.setItemBatchRecord(itemBatchRecord);
		organizationItemProcessRecord.setItemProcess(itemProcess);
		organizationItemProcessRecord.setOperatorId(operator.getId());
		organizationItemProcessRecord.setOperatorNickname(operator.getNickname());
		itemProcessRecordService.save(organizationItemProcessRecord);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProcessRecord_html")
	@RequestMapping("/delOrganizationItemProcessRecord")
	public ResultDto delOrganizationItemProcessRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		List<ItemProcessRecord> itemProcessRecords = itemProcessRecordService.findAllById(l_ids);
		for(ItemProcessRecord itemProcessRecord : itemProcessRecords) {
			if(itemProcessRecord.getItemBatchRecord().getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("存在非本机构流程记录",null,2);
			}
		}

		itemProcessRecordService.delete(itemProcessRecords);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProcessRecord_html")
	@RequestMapping("/getOrganizationItemProcessRecordPage")
	public TableDto<ItemProcessRecordDto> getOrganizationItemProcessRecordPage(Integer pageNumber,Integer pageSize,long itemId,long itemProductionLineId,long itemProcessId,String search) {
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(itemProductionLineId);
		Item item = itemProductionLine.getItem();
		if(item.getId() != itemId) {
			return null;
		}
		ItemProcess itemProcess = itemProcessService.getOne(itemProcessId);
		if(itemProcess.getItemProductionLine().getId()!=itemProductionLineId) {
			return null;
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		if(item.getOrganization().getId()!=organization.getId()) {
			return null;
		}
		Page<ItemProcessRecord> pageResult = itemProcessRecordService.getItemProcessRecordPage(pageNumber, pageSize, itemId, itemProductionLineId, itemProcessId, search);
		List<ItemProcessRecord> organizationItemProcessRecords = pageResult.getContent();
		List<ItemProcessRecordDto> organizationItemProcessRecordDtos = new ArrayList<ItemProcessRecordDto>();
		organizationItemProcessRecords.forEach(organizationItemProcessRecord->organizationItemProcessRecordDtos.add(new ItemProcessRecordDto().convert(organizationItemProcessRecord)));
		return new TableDto<ItemProcessRecordDto>(organizationItemProcessRecordDtos,pageResult.getTotalElements());
	}
	
}
